<template>
	<view class="sign-box">
		<view class="sign-title">
			<image src="./sign.png" mode=""></image>
		</view>
		<!-- 签到面板 -->
		<view class="sign-panel">
			<view class="sgin-item-box">
				<view class="sign-item" v-for="(item,index) in signList" :key="index" :class="item.isSignIn ?'signed':''" @click="userReplaceSignIn(item)">
					<view class="sign-text">{{index + 1}}</view>
				</view>
				<span class="register_btn" @click="register">签 到</span>
			</view>
		</view>
		<view class="sign-progress-box">
			<view class="sign-progress-bar">
				<view class="sign-progress-bar-line" :style="{width:percentage + '%'}"></view>
			</view>
			<view class="sign-progress-data">
				<view class="data-item">7天</view>
				<view class="data-item">14天</view>
				<view class="data-item">21天</view>
				<view class="data-item">28天</view>
			</view>
		</view>
		<view class="sign-gift-box">
			<view class="sign-gift-item" v-for="item in awardList" :key='item.pid' @click="getAward(item)">
				<view class="gift-box-item-up">
					<view class="gift-box-item-left">
						{{item.pid}}天
					</view>
					<view class="gift-box-item-right">
						<image class="img-box" :src="item.img"></image>
						<view class="gift-name">
							{{item.pname}}
						</view>
					</view>
				</view>
				<view class="sign-gift-btn"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSignInCondit,
		getSignInAward,
		userRegister,
		getSignInProgress,
		userReplace,
		userGetAward
	} from "../../api/user.js"
	export default {
		data() {
			return {
				signList: [], // 签到列表
				awardList: [], // 奖励列表
				percentage:0, // 占比情况
			}
		},
		computed: {
			userInfo() {
				return this.$store.getters.userInfo;
			},
		},
		mounted() {
			this.init()
		},
		methods: {
			init(){
				this.getUserSign()
				this.getAwardList()
				
			},
			// 获取用户签到情况
			getUserSign() {
				let targetId = this.userInfo.id;
				getSignInCondit(targetId).then((res) => {
					if (res.code == '200') {
						this.signList = res.data;
						this.getProgress()
					} else {
						this.signList = []
					}
				})
			},
			//用户签到奖励列表
			getAwardList() {
				let targetId = this.userInfo.id;
				getSignInAward(targetId).then((res) => {
					if (res.code == '200') {
						this.awardList = res.data
					} else {
						this.awardList = []
					}
				})
			},
			getProgress(){
				let targetId = this.userInfo.id;
				getSignInProgress(targetId).then((res) =>{
					if (res.code == '200') {
						let longDay = res.data.longDay;
						let total = this.signList.length;
						this.percentage = parseInt((longDay / total).toFixed(2) * 100) 
					}
				})
			},
			// 用户签到
			register() {
				let data = {
					targetId: this.userInfo.id,
				}
				userRegister(data).then((res) => {
					if (res.code == '200') {
						uni.showToast({
							title: res.message,
							duration: 2000
						}); 
						this.getUserSign()
					}else{
						uni.showToast({
							title: res.message,
							duration: 2000,
							icon:"error"
						}); 
					}
				})
			},
			// 用户补签
			userReplaceSignIn(item){
				let data = {
					targetId: this.userInfo.id,
					day:item.day
				}
				userReplace(data).then((res) =>{
					if(res.code == '200'){
						uni.showToast({
							title: res.message,
							duration: 2000,
						}); 
						this.getUserSign()
					}else{
						uni.showToast({
							title: res.message,
							duration: 2000,
							icon:"error"
						}); 
					}
				})
			},
			// 领取签到奖励
			getAward(item){
				let data = {
					targetId: this.userInfo.id,
					pid:item.pid
				}
				userGetAward(data).then((res) =>{
					if(res.code == '200'){
						uni.showToast({
							title: res.message,
							duration: 2000,
						}); 
					}else{
						uni.showToast({
							title: res.message,
							duration: 2000,
							icon:"error"
						}); 
					}
				})
			},
			
			
		}
	}
</script>

<style lang="scss" scoped>
	.sign-box {
		background: #090919 url('./bg.png') no-repeat;
		background-size: 100% auto;
		min-height: 100vh;
	}

	.sign-title {
		padding-top: 4.5rem;
		color: #fff;
		font-size: 1rem;
		font-weight: 500;
		text-align: center;
		letter-spacing: 2px;
		margin-bottom: 1rem;
	}

	.sign-title image {
		width: 50%;
		height: 90rpx;
	}

	.sign-panel {
		width: 80%;
		margin: 0 auto;
		padding: 1.4rem 0.38rem;
		border-radius: 0.24rem;
		overflow: hidden;
		background-color: #fff;
	}

	.sgin-item-box {
		display: grid;
		grid-template-columns: repeat(auto-fill, 12%);
		row-gap: 10px;
		column-gap: 0.4rem;
		justify-content: center;
		// line-height: 100%;
		height: 12.5rem;
		padding-bottom: 100rpx;
		position: relative;

		.register_btn {
			position: absolute;
			bottom: 0;
			height: 80rpx;
			line-height: 80rpx;
			left: 50%;
			transform: translate(-50%, 0);
			width: 200rpx;
			text-align: center;
			border-radius: 40rpx;
			background-image: url(../../static/fontSize.png);
			background-size: 100%;
			background-repeat: no-repeat;

		}
	}

	.sign-item {
		background: url('./unsign.png') no-repeat;
		background-size: 100% 100%;

		&.signed {
			background: url('./signed.png') no-repeat;
			background-size: 100% 100%;

			.sign-text {
				display: none;
			}
		}
	}

	.sign-text {
		text-align: center;
		font-size: 0.4rem;
		padding-top: 0.2rem;
		color: #ff8d1a;
	}

	.sign-progress {
		&-box {
			padding: 1rem 0;
			width: 80%;
			margin: 0 auto;
		}

		&-bar {
			height: 0.6rem;
			background: #312176;
			border-radius: 50px;
			border: 2px solid #6c6c76;
		}

		&-bar-line {
			height: 100%;
			background: linear-gradient(to right, #6c43ff, #956aff);
			border-radius: 50px;
		}

		&-data {
			padding-top: .5rem;
			display: flex;
			justify-content: end;
		}

		&-box {
			.data-item {
				width: 25%;
				color: #fff;
				text-align: right;
			}
		}
	}

	.sign-gift-box {
		padding: 1rem 0.5rem;
		background: linear-gradient(to right bottom, #090919, #233a47);
		overflow: hidden;

		&::after {
			content: '';
			clear: both;
		}

		.sign-gift-item {
			width: calc(50% - 20px);
			float: left;
			margin: 0 10px 10px;
			box-sizing: border-box;
			border: 1px solid #6c43ff;
			border-radius: 4px;
			overflow: hidden;
		}

		.sign-gift-btn {
			width: 100%;
			height: 2rem;
			background: url('./btn.png') no-repeat;
			background-size: cover;
		}

		.gift-box-item-up {
			display: flex;
			height: 2.8rem;
			align-items: center;
			background-color: #38334d;
		}

		.gift-box-item-left {
			display: block;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 2.8rem;
			height: 100%;
			text-align: center;
			color: #fff;
			white-space: nowrap;
			border-right: 1px solid #ba8dfe;
		}

		.gift-box-item-right {
			display: flex;
			flex: 1;
			overflow: hidden;
		}

		.img-box {
			width: 2rem;
			height: 2rem;
			overflow: hidden;
		}

		.gift-name {
			flex: 1;
			display: flex;
			align-items: center;
			font-size: .8rem;
			color: #fff;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			box-sizing: border-box;
			margin: 0 20rpx;
		}
	}
</style>